package web.directories.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import domain.entities.MiembroBms;
import domain.entities.Role;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import services.CriteriaStaff;
import services.RoleCRUDService;
import services.StaffCRUDService;

import java.util.ArrayList;
import java.util.List;

@Namespace("/Directories/Staff")
public class StaffController extends ActionSupport implements ModelDriven {

    private StaffCRUDService service;
    private RoleCRUDService roleCRUDService;
    private MiembroBms staff = new MiembroBms();
    private MiembroBms editableStaff = new MiembroBms();
    private List<MiembroBms> list;
    private List<Role> roles;
    private String editableStaffId;

    @Autowired
    public StaffController(StaffCRUDService service,RoleCRUDService roleCRUDService) {
        this.service = service;
        this.roleCRUDService = roleCRUDService;
    }

    @Override
    public Object getModel() {
        return staff;
    }

    @Action(value="main", results={
            @Result(name="success", location="/directories.staff.tiles", type="tiles")
    })
    public String execute() {
        list = new ArrayList<MiembroBms>(0);
        return SUCCESS;
    }

    @Action(value="list", results={
            @Result(name="success", location="/directories.staff.tiles", type="tiles")
    })
    public String list() {

        CriteriaStaff cr = new CriteriaStaff(staff);
        list = service.list(cr);

        return SUCCESS;
    }

    @Action(value="create", results={
            @Result(name="success",location="/directories.staff.create.tiles", type="tiles")
    })
    public String create() {
        if(editableStaffId != null){
            int id = Integer.parseInt(editableStaffId);
            staff = service.findById(id);
        }

        return SUCCESS;
    }

    @Action(value="save", results={
            @Result(name="success",location="main", type="redirectAction")
    })
    public String save() {
        Role role = roleCRUDService.getRoleById(staff.getRole().getId());
        staff.setRole(role);
        service.save(staff);

        return SUCCESS;
    }

    @Action(value="delete", results={
            @Result(name="success",location="main", type="redirectAction")
    })
    public String deleteLaboratory() {
        service.delete(staff);
        return SUCCESS;
    }

    public MiembroBms getStaff() {
        return staff;
    }

    public void setStaff(MiembroBms staff){
        this.staff = staff;
    }

    public List<MiembroBms> getList() {
        return list;
    }

    public void setList(List<MiembroBms> list) {
        this.list = list;
    }

    public MiembroBms getEditableStaff() {
        return editableStaff;
    }

    public void setEditableStaff(MiembroBms editableStaff) {
        this.editableStaff = editableStaff;
    }

    public String getEditableStaffId() {
        return editableStaffId;
    }

    public void setEditableStaffId(String editableStaffId) {
        this.editableStaffId = editableStaffId;
    }

    public List<Role> getRoles() {
        return roleCRUDService.findStaffRole();
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

}
